<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="68px">
      <!-- <el-form-item label="挂号编号" prop="registerId">
        <el-input v-model="registerId" placeholder="请输入挂号编号" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery2">搜索</el-button>
      </el-form-item> -->
      <el-form-item label="身份证" prop="patientCardId">
        <el-input v-model="patientCardId" placeholder="请输入身份证" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>


    <!-- 显示住院单信息 -->
    <el-card v-if="cardFlag" style="margin: 20px auto; width: 85%;">
      <el-descriptions title="住院单信息">
        <el-descriptions-item label="挂号id">{{ enter.registerId }}</el-descriptions-item>
        <el-descriptions-item label="住院号">{{ enter.enterId }}</el-descriptions-item>
        <el-descriptions-item label="患者姓名">{{ enter.patientName }}</el-descriptions-item>
        <el-descriptions-item label="科室">{{ enter.depName }}</el-descriptions-item>
        <el-descriptions-item label="办理医生">{{ enter.userName }}</el-descriptions-item>
        <el-descriptions-item label="办理时间">{{ enter.createTime }}</el-descriptions-item>
        <el-descriptions-item label="入住状态">{{ enter.bedId == null ? '未入院' : (enter.outData == null ? '已入院' : '已出院') }}</el-descriptions-item>
        <el-descriptions-item label="床位id">{{ enter.bedId }}</el-descriptions-item>
        <el-descriptions-item label="入院时间">{{ enter.enterDate }}</el-descriptions-item>
        <el-descriptions-item label="出院时间" v-if="enter.outData != null">{{ enter.outData }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ enter.remark }}</el-descriptions-item>
        <el-descriptions-item v-if="enter.bedId == null">
          <el-button type="success" size="small" @click="handleFbed">分配床位</el-button>
        </el-descriptions-item>
        <el-descriptions-item v-if="enter.bedId != null && enter.outData == null">
          <el-button type="success" size="small" @click="$message.error('暂未实现')">申请手术</el-button>
        </el-descriptions-item>
        <el-descriptions-item v-if="enter.bedId != null && enter.outData == null">
          <el-button type="success" size="small" @click="handleChuYuan">出院</el-button>
        </el-descriptions-item>
        <!-- <el-descriptions-item>
                <el-button type="success" size="small" @click="endRegister">结束挂号单</el-button>
              </el-descriptions-item>
              <el-descriptions-item>
                <el-button type="success" size="small" @click="zhuYuan">住院</el-button>
              </el-descriptions-item> -->
      </el-descriptions>
    </el-card>


    <!-- 分配床位对话框 -->
    <el-dialog title="分配床位" :visible.sync="bedDialogFormVisible" style="width: 50%; margin: auto;">
      <el-form label-width="80px">
        <el-form-item label="床位" prop="bedId">
          <el-select v-model="bedId" placeholder="请选择床位">
            <el-option v-for="item in bedList" :key="item.bedId" :label="item.bedNum" :value="item.bedId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="bedDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="fBed()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getEnterByCardId, getBedListByDepId, goFBed, goChuYuan } from '@/api/customer-serv/enter'

export default {
  data() {
    return {
      queryParams: {},
      bedDialogFormVisible: false,
      cardFlag: false,
      patientCardId: "",
      enter: {},
      bedList: [],
      bedId: ""
    };
  },
  methods: {
    handleQuery() {
      if (this.patientCardId == "") {
        this.$message.error("请输入身份证号");
        return;
      }
      getEnterByCardId(this.patientCardId).then((result) => {
        if (result.message == "no") {
          this.$message.error("未查询到相关住院单");
          return;
        }
        this.enter = result.data.enter;
        this.cardFlag = true;
      });
    },
    handleFbed() {
      getBedListByDepId(this.enter.depId).then((result) => {
        this.bedList = result.data.bedList;
        this.bedDialogFormVisible = true;
      });
    },
    fBed() {
      if (this.bedId == "") {
        this.$message.error("请选择床位");
        return;
      }
      this.enter.bedId = this.bedId;
      goFBed(this.enter).then((result) => {
        this.$message.success("床位分配成功，入院成功！");
        this.enter = result.data.enter;
        this.bedDialogFormVisible = false;
      });
    },
    handleChuYuan() {
      goChuYuan(this.enter).then((result) => {
        this.$message.success("已出院，相应病床已释放");
        this.enter = result.data.enter;
      });
    }
  },
};
</script>